package com.hudongwx.sadpw.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class FoodDetailActivity extends AppCompatActivity {
    MyRecycleAdapterTuiJian adapter;
    JSONObject jobj;
    Intent intent;
    String type;
    String TAG="FoodDetailActivity";
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        recyclerView= (RecyclerView) findViewById(R.id.detail_rec);
        parseShan();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        Log.i(TAG, "onCreate:gridLayoutManager::::::: "+(null==gridLayoutManager));
        Log.i(TAG, "onCreate:recyclerView>>>>>>>>>> "+(null==recyclerView));
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter=new MyRecycleAdapterTuiJian(jobj);
        recyclerView.setAdapter(adapter);

    }
    //recycleAdapter
    class MyRecycleAdapterTuiJian extends RecyclerView.Adapter<MyRecycleViewHolderTuiJian>{
        JSONObject jobj;

        public MyRecycleAdapterTuiJian(JSONObject jobj) {
            this.jobj = jobj;
        }
        public void updateC2(JSONObject jobj){
            this.jobj=jobj;
            notifyDataSetChanged();
        }

        //        public MyRecycleAdapterTuiJian(JSONArray jsonArray1) {
//            this.jsonArray1 = jsonArray1;
//        }
//        public void updateC2(JSONArray jasonArray1){
//            this.jsonArray1=jasonArray1;
//            notifyDataSetChanged();
//        }
        @Override
        public MyRecycleViewHolderTuiJian onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.detail_food, null);
            return new MyRecycleViewHolderTuiJian(view);
        }

        @Override
        public void onBindViewHolder(MyRecycleViewHolderTuiJian holder, int position) {
            if (null==jobj){
                recyclerView.setVisibility(View.GONE);
            }else {
                try {
                    x.image().bind(holder.imageView1,jobj.getString("ImgUrl"));
                    holder.textView8.setText(jobj.getString("ProductName"));
                    holder.textView1.setText(jobj.getString("ShortName"));
                    holder.textView2.setText(jobj.getString("UnitPrice"));
                    holder.textView4.setText(jobj.getString("Spec"));
                    holder.textView5.setText(jobj.getString("UnitPrice"));
                    holder.textView7.setText(jobj.getString("ProductAttributes"));
                    holder.webView.loadDataWithBaseURL("about:blank",jobj.getString("AppDescribe"), "text/html", "utf-8",null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (null==jobj)return 0;
            return 1;
        }
    }
    class MyRecycleViewHolderTuiJian extends RecyclerView.ViewHolder {
        TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7,textView8;
        WebView webView;
        ImageView imageView1;
        public MyRecycleViewHolderTuiJian(View itemView) {
            super(itemView);
            textView1= (TextView) itemView.findViewById(R.id.detail_tv_1);
            textView2= (TextView) itemView.findViewById(R.id.detail_tv_2);
            textView3= (TextView) itemView.findViewById(R.id.detail_tv_3);
            textView4= (TextView) itemView.findViewById(R.id.detail_tv_4);
            textView5= (TextView) itemView.findViewById(R.id.detail_tv_5);
            textView6= (TextView) itemView.findViewById(R.id.detail_tv_6);
            textView7= (TextView) itemView.findViewById(R.id.detail_tv_7);
            textView8= (TextView) itemView.findViewById(R.id.detail_tv_8);
            webView= (WebView) itemView.findViewById(R.id.detail_web);
            imageView1= (ImageView) itemView.findViewById(R.id.detail_iv_1);

        }
    }
    //解析靓贴
    public void parseShan(){
        intent = getIntent();
        type = intent.getStringExtra("type");
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Product?productId="+type);
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONObject data1 = result.getJSONObject("Data");

                    Log.i(TAG, "onSuccess: data___detail:"+data1.toString());
                    adapter.updateC2(data1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}